package com.zbcz.erp.util

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.graphics.Outline
import android.graphics.Rect
import android.os.Build
import android.os.SystemClock
import android.util.Log
import android.view.TouchDelegate
import android.view.View
import android.view.ViewOutlineProvider
import com.zbcz.erp.R
import java.util.concurrent.TimeUnit

fun View.setRoundedCorners(radius: Float) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        outlineProvider = object : ViewOutlineProvider() {
            override fun getOutline(view: View?, outline: Outline?) {
                outline?.setRoundRect(0, 0, view!!.width, view.height, radius)
            }
        }
        clipToOutline = true
    }
}


fun adjustLayoutForStatusBar(context: Context, mainLayout: View) {
    val statusBarHeight = getStatusBarHeight(context)
//    val params = mainLayout.layoutParams as ViewGroup.MarginLayoutParams
//    params.setMargins(0, statusBarHeight, 0, 0) // Left, Top, Right, Bottom
//    mainLayout.layoutParams = params
    mainLayout.setPadding(0, statusBarHeight, 0, 0)
}

fun getStatusBarHeight(context: Context): Int {
    var statusBarHeight = 0
    val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
    if (resourceId > 0) {
        statusBarHeight = context.resources.getDimensionPixelSize(resourceId)
    }
    return statusBarHeight
}

fun View.onSingleClick(
    interval: Int? = SingleClickUtils.singleClickInterval,
    isShareSingleClick: Boolean? = true,
    listener: View.OnClickListener? = null
) {
    if (listener == null) {
        return
    }

    setOnClickListener {
        determineTriggerSingleClick(
            interval ?: SingleClickUtils.singleClickInterval, isShareSingleClick
                ?: true, listener
        )
    }
}

/**
 * Determine whether to trigger a single click.
 *
 * @param interval Single click interval.Unit is [TimeUnit.MILLISECONDS].
 * @param isShareSingleClick True if this view is share single click interval whit other view
 *   in same Activity, false otherwise.
 * @param listener Single click listener.
 */
fun View.determineTriggerSingleClick(
    interval: Int = SingleClickUtils.singleClickInterval,
    isShareSingleClick: Boolean = false,
    listener: View.OnClickListener
) {
    val target = if (isShareSingleClick) getActivity(this)?.window?.decorView ?: this else this
    val millis = target.getTag(R.id.single_click_tag_last_single_click_millis) as? Long ?: 0
    if (SystemClock.uptimeMillis() - millis >= interval) {
        target.setTag(R.id.single_click_tag_last_single_click_millis, SystemClock.uptimeMillis())
        listener.onClick(this)
    } else {
        Log.i("determineTriggerSingleClick", "determineTriggerSingleClick intercept")
    }
}

 fun getActivity(view: View): Activity? {
    var context = view.context
    while (context is ContextWrapper) {
        if (context is Activity) {
            return context
        }
        context = context.baseContext
    }
    return null
}

/**
 * 扩充View点击范围
 */
fun expandTouchArea(view: View, padding: Int, parentView: View) {
    view.post {
        val bounds = Rect()
        view.getHitRect(bounds)
        bounds.left -= padding
        bounds.top -= padding
        bounds.right += padding
        bounds.bottom += padding
        Log.i("TAG", "onFragmentResult bounds：$bounds")
        val touchDelegate = TouchDelegate(bounds, view)
        parentView.touchDelegate = touchDelegate
    }
}


